package com.wanggsx.library.base.view;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

import com.wanggsx.library.base.app.needextends.SuperAppInstance;
import com.wanggsx.library.util.UtilsAntiShake;
import com.wanggsx.library.util.UtilsKeyboard;
import com.wanggsx.library.util.UtilsToast;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected View mContentView;
    protected Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mActivity = this;
        SuperAppInstance.mListActivitys.add(this);
        super.onCreate(savedInstanceState);
        if (setRootLayoutId()>0){
            mContentView = LayoutInflater.from(this).inflate(setRootLayoutId(), null);
            setContentView(mContentView);

        }else if (setRootLayoutId()==0){
            UtilsToast.showShort("请给页面 " + this.getClass().getSimpleName() +" 设置RootLayout!");
        }
        initView(savedInstanceState, mContentView);
        setListener();
        initData(getIntent().getExtras());
        doBusiness();
    }

    @Override
    public void onClick(View v) {
        if (UtilsAntiShake.isValid(v)) {
            onWidgetClick(v);
        }
    }

    @Override
    protected void onDestroy() {
        SuperAppInstance.mListActivitys.remove(this);
        UtilsKeyboard.fixSoftInputLeaks(this);
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (onBack()){
            super.onBackPressed();
        }
    }

    public abstract int setRootLayoutId();
    public abstract void initView(Bundle savedInstanceState,View contentView);
    public abstract void setListener();
    public abstract void initData(Bundle bundle);
    public abstract void doBusiness();
    public abstract void onWidgetClick(View view);
    /** 返回true则表示实现类拦截了点击事件 */
    public abstract boolean onBack();
}
